package com.finalBlog.util;

import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

import com.jfinal.kit.StrKit;

/**
 * 类/接口信息描述:通用工具类
 *
 * @author dengxc
 *
 */
public class CommonTools {
	/**
	* 方法描述：获取随机数字或字母字符串
	*
	* @param length 字符串长度
	* @return
	* String
	*/
	public static String getCharAndNum(int length){
		String value = "";
		Random rand = new Random();
		for(int i =0;i<length;i++){
			String charOrNum = rand.nextInt(2)%2==0?"char":"num";//数字还是字母
			if("char".equals(charOrNum)){
				int vcase = rand.nextInt(2)%2==0?65:97;//大写还是小写
				char v = (char) (vcase+rand.nextInt(26));
				value += v;
			}else{
				value += rand.nextInt(10);
			}
		}
		return value;
	}
	/**
	* 方法描述：获取访问者ip
	*
	* @param request
	* @return
	* String
	*/
	public static String getIP(HttpServletRequest request){
		Object ip = null;
		try {
			 ip = request.getHeader(
					"X-Forwarded-For".toLowerCase());
		} catch (Exception e) {
			return "127.0.0.1";
		}
		if (ip == null || "unknown".equalsIgnoreCase(ip.toString())) {
			ip = request.getHeader(
					"Proxy-Client-IP".toLowerCase());
		}
		if (ip == null || "unknown".equalsIgnoreCase(ip.toString())) {
			ip = request.getHeader(
					"WL-Proxy-Client-IP".toLowerCase());
		}
		if (ip == null || "unknown".equalsIgnoreCase(ip.toString())) {
			ip = request.getHeader(
					"HTTP_CLIENT_IP".toLowerCase());
		}
		if (ip == null || "unknown".equalsIgnoreCase(ip.toString())) {
			ip = request.getHeader(
					"HTTP_X_FORWARDED_FOR".toLowerCase());
		}
		if (ip == null || "unknown".equalsIgnoreCase(ip.toString())) {
			ip = request.getRemoteAddr();
		}
		return ip.toString();
	}
	/**
	* 方法描述：把请求对象中的cookie 放进map中
	*
	* @param request
	* @return
	* Map<String,Cookie>
	*
	*/
	public static Map<String, String> getCookieMap(HttpServletRequest request){
		Map<String, String> cookiemap = new HashMap<String,String>();
		Cookie[] cookies = request.getCookies();
		if(cookies!=null&&cookies.length>0){
			try {
				for(Cookie cookie:cookies){
					cookiemap.put(cookie.getName(), URLDecoder.decode(cookie.getValue(), "utf-8"));
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if(!cookiemap.containsKey("themstype")){
			cookiemap.put("themstype", "default");//默认主题
		}
		return cookiemap;
	}
	/**
	* 方法描述：获取请求参数
	*
	* @param request
	* @return
	* Map<String,String>
	*/
	public static Map<String, String[]> getParamMap(HttpServletRequest request){
		Map<String, String[]> param = request.getParameterMap();
		return param;
	}
	/**
	 * 返回两个日期之间隔了多少小时
	 * 
	 * @param date1
	 * @param end
	 * @return
	 */
	public static int getDateHourSpace(Date start, Date end) {
		int hour = (int) ((start.getTime() - end.getTime()) / 3600 / 1000);
		return hour;
	}
	
	/**
	 * 返回两个日期之间隔了多少天
	 * 
	 * @param date1
	 * @param end
	 * @return
	 */
	public static int getDateDaySpace(Date start, Date end) {
		int day = (int) (getDateHourSpace(start, end) / 24);
		return day;
	}
	/*public static Object getMapValue(Map map,String key){
		if(map==null) 
			return null;
		Object o = map.get(key);
		return o;
	}*/
	
	public static Date parseDate(String date,String format)throws Exception{
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Date d = sdf.parse(date);
		return d;
	}
	public static String formatDate(Date d,String format) throws Exception{
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		String date = sdf.format(d);
		return date;
	}
	public static boolean isPhoneNum(String num){
		if(StringUtils.isEmpty(num))
			return false;
		final String regex = "^((13[0-9])|(15[4,\\D])|(18[0,1,5-9])|(14[5,7])|(17[0,7]))\\d{8}$"; 
		Pattern pattern = Pattern.compile(regex);
		Matcher mat = pattern.matcher(num);
		return mat.matches();
	}
	public static boolean regStrs(String str){
		String regex = "(\\d+,?\\d*)+";
		Pattern p  = Pattern.compile(regex);
		Matcher m = p.matcher(str);
		return m.matches();
	}
	public static String getFirstPara(Map<String, String[]> paramap, String key){
		String[] arr = paramap.get(key);
		if(arr!=null){
			return StrKit.notBlank(arr[0])?arr[0]:null;
		}
		return null;
	}
	/**
	* 方法描述：判断请求是否是ajax请求
	*
	* @param request
	* @return
	* boolean
	*
	* 2015年12月4日
	*/
	public static boolean isAjax(HttpServletRequest request){
	    return  (request.getHeader("X-Requested-With") != null  && "XMLHttpRequest".equals( request.getHeader("X-Requested-With").toString())   ) ;
	}
}
